home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PEAR / Command / Auth.php next >
PHP Script  |  2004-10-01  |  5KB  |  155 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <ssb@php.net>                                    |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Auth.php,v 1.15 2004/01/08 17:33:13 sniper Exp $
  20.  
  21. require_once "PEAR/Command/Common.php";
  22. require_once "PEAR/Remote.php";
  23. require_once "PEAR/Config.php";
  24.  
  25. /**
  26.  * PEAR commands for managing configuration data.
  27.  *
  28.  */
  29. class PEAR_Command_Auth extends PEAR_Command_Common
  30. {
  31.     // {{{ properties
  32.  
  33.     var $commands = array(
  34.         'login' => array(
  35.             'summary' => 'Connects and authenticates to remote server',
  36.             'shortcut' => 'li',
  37.             'function' => 'doLogin',
  38.             'options' => array(),
  39.             'doc' => '
  40. Log in to the remote server.  To use remote functions in the installer
  41. that require any kind of privileges, you need to log in first.  The
  42. username and password you enter here will be stored in your per-user
  43. PEAR configuration (~/.pearrc on Unix-like systems).  After logging
  44. in, your username and password will be sent along in subsequent
  45. operations on the remote server.',
  46.             ),
  47.         'logout' => array(
  48.             'summary' => 'Logs out from the remote server',
  49.             'shortcut' => 'lo',
  50.             'function' => 'doLogout',
  51.             'options' => array(),
  52.             'doc' => '
  53. Logs out from the remote server.  This command does not actually
  54. connect to the remote server, it only deletes the stored username and
  55. password from your user configuration.',
  56.             )
  57.  
  58.         );
  59.  
  60.     // }}}
  61.  
  62.     // {{{ constructor
  63.  
  64.     /**
  65.      * PEAR_Command_Auth constructor.
  66.      *
  67.      * @access public
  68.      */
  69.     function PEAR_Command_Auth(&$ui, &$config)
  70.     {
  71.         parent::PEAR_Command_Common($ui, $config);
  72.     }
  73.  
  74.     // }}}
  75.  
  76.     // {{{ doLogin()
  77.  
  78.     /**
  79.      * Execute the 'login' command.
  80.      *
  81.      * @param string $command command name
  82.      *
  83.      * @param array $options option_name => value
  84.      *
  85.      * @param array $params list of additional parameters
  86.      *
  87.      * @return bool TRUE on success, FALSE for unknown commands, or
  88.      * a PEAR error on failure
  89.      *
  90.      * @access public
  91.      */
  92.     function doLogin($command, $options, $params)
  93.     {
  94.         $server = $this->config->get('master_server');
  95.         $remote = new PEAR_Remote($this->config);
  96.         $username = $this->config->get('username');
  97.         if (empty($username)) {
  98.             $username = @$_ENV['USER'];
  99.         }
  100.         $this->ui->outputData("Logging in to $server.", $command);
  101.         
  102.         list($username, $password) = $this->ui->userDialog(
  103.             $command,
  104.             array('Username', 'Password'),
  105.             array('text',     'password'),
  106.             array($username,  '')
  107.             );
  108.         $username = trim($username);
  109.         $password = trim($password);
  110.         
  111.         $this->config->set('username', $username);
  112.         $this->config->set('password', $password);
  113.         
  114.         $remote->expectError(401);
  115.         $ok = $remote->call('logintest');
  116.         $remote->popExpect();
  117.         if ($ok === true) {
  118.             $this->ui->outputData("Logged in.", $command);
  119.             $this->config->store();
  120.         } else {
  121.             return $this->raiseError("Login failed!");
  122.         }
  123.  
  124.     }
  125.  
  126.     // }}}
  127.     // {{{ doLogout()
  128.  
  129.     /**
  130.      * Execute the 'logout' command.
  131.      *
  132.      * @param string $command command name
  133.      *
  134.      * @param array $options option_name => value
  135.      *
  136.      * @param array $params list of additional parameters
  137.      *
  138.      * @return bool TRUE on success, FALSE for unknown commands, or
  139.      * a PEAR error on failure
  140.      *
  141.      * @access public
  142.      */
  143.     function doLogout($command, $options, $params)
  144.     {
  145.         $server = $this->config->get('master_server');
  146.         $this->ui->outputData("Logging out from $server.", $command);
  147.         $this->config->remove('username');
  148.         $this->config->remove('password');
  149.         $this->config->store();
  150.     }
  151.  
  152.     // }}}
  153. }
  154.  
  155. ?>